package com.group10.service.impl;

import com.group10.entity.Blog;
import com.group10.entity.Result;
import com.group10.mapper.BlogMapper;
import com.group10.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import static com.group10.util.TimeUtil.getNow;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private UserServiceImpl userService;

    @Override
    public Result<?> getAllBlog() {
        return Result.success(blogMapper.getAllBlog());
    }

    @Override
    public Result<?> getUserAllBlog(String token) {
        String userId = (String) userService.userInfo(token).get("id");
        return Result.success(blogMapper.getUserAllBlog(userId));
    }

    @Override
    public Result<?> newBlog(String token,Blog blog) {
        String userId = (String) userService.userInfo(token).get("id");
        blog.setUserId(userId);
        blog.setTime(getNow());
        int flag = blogMapper.newBlog(blog);
        if (flag > 0){
            return Result.success();
        }else{
            return Result.error("新建失败");
        }
    }

    @Override
    public Result<?> deleteBlog(int id) {
        int flag = blogMapper.deleteBlog(id);
        if (flag > 0){
            return Result.success();
        }else{
            return Result.error("删除失败");
        }
    }
}
